﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerTheoDoiThiDuaDao : ITheoDoiThiDuaDao
    {
        public SqlServerTheoDoiThiDuaDao()
        {
            SQLUtility.TableName = "TheoDoiThiDua";
        }

        //Trả về đối tượng báo ăn
        private TheoDoiThiDua MakeTheoDoiThiDua(DataRow row)
        {
            Guid _id = Guid.Parse(row["ThiDuaID"].ToString());
            Guid _nhanVienID = Guid.Parse(row["NhanVienID"].ToString());
            Guid _classID = Guid.Parse(row["ClassID"].ToString());
            Guid _schoolYearID = Guid.Parse(row["SchoolYearID"].ToString());
            string _thang1 = row["Thang1"].ToString();
            string _thang2 = row["Thang2"].ToString();
            string _thang3 = row["Thang3"].ToString();
            string _thang4 = row["Thang4"].ToString();
           
            string _thang9 = row["Thang9"].ToString();
            string _thang10 = row["Thang10"].ToString();
            string _thang11 = row["Thang11"].ToString();
            string _thang12 = row["Thang12"].ToString();
            string _xepLoai = row["XepLoai"].ToString();
            string _ghiChu = row["GhiChu"].ToString();


            return new TheoDoiThiDua(_id, _nhanVienID,_classID,_schoolYearID,_thang1,_thang2,_thang3,_thang4,_thang9,_thang10,_thang11,_thang12,_xepLoai,_ghiChu);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<TheoDoiThiDua> MakeTheoDoiThiDuas(DataTable dt)
        {
            IList<TheoDoiThiDua> list = new List<TheoDoiThiDua>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeTheoDoiThiDua(row));
            }
            return list;
        }
        private DbParameter[] Take(TheoDoiThiDua TheoDoiThiDua)
        {
            DbParameter[] parameters = new DbParameter[14];
            parameters[0] = new SqlParameter("@ThiDuaID", TheoDoiThiDua.Id);
            parameters[1] = new SqlParameter("@NhanVienID", TheoDoiThiDua.NhanVienID);
            parameters[2] = new SqlParameter("@ClassID", TheoDoiThiDua.ClassID);

            parameters[3] = new SqlParameter("@SchoolYearID", TheoDoiThiDua.SchoolYearID);
            parameters[4] = new SqlParameter("@Thang1", TheoDoiThiDua.Thang1);
            parameters[5] = new SqlParameter("@Thang2", TheoDoiThiDua.Thang2);
            parameters[6] = new SqlParameter("@Thang3", TheoDoiThiDua.Thang3);

            parameters[7] = new SqlParameter("@Thang4", TheoDoiThiDua.Thang4);
           
            parameters[8] = new SqlParameter("@Thang9", TheoDoiThiDua.Thang9);
            parameters[9] = new SqlParameter("@Thang10", TheoDoiThiDua.Thang10);
            parameters[10] = new SqlParameter("@Thang11", TheoDoiThiDua.Thang11);
            parameters[11] = new SqlParameter("@Thang12", TheoDoiThiDua.Thang12);
            parameters[12] = new SqlParameter("@XepLoai", TheoDoiThiDua.XepLoai);
            parameters[13] = new SqlParameter("@GhiChu", TheoDoiThiDua.GhiChu);
            return parameters;
        }

        public IList<Business_Objects.TheoDoiThiDua> GetTheoDoiThiDua()
        {
            SQLUtility.TableName = "TheoDoiThiDua";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeTheoDoiThiDuas(dt);
        }

        public Business_Objects.TheoDoiThiDua GetTheoDoiThiDuaByID(string TheoDoiThiDuaID)
        {
            SQLUtility.TableName = "TheoDoiThiDua";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@ThiDuaID", TheoDoiThiDuaID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeTheoDoiThiDua(row);
            }
        }

        public void InsertTheoDoiThiDua(Business_Objects.TheoDoiThiDua TheoDoiThiDua)
        {
            SQLUtility.TableName = "TheoDoiThiDua";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(TheoDoiThiDua), true);
        }

        public int UpdateTheoDoiThiDua(Business_Objects.TheoDoiThiDua TheoDoiThiDua)
        {
            SQLUtility.TableName = "TheoDoiThiDua";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(TheoDoiThiDua)); }
            catch { return -1; }
        }

        public int DeleteTheoDoiThiDua(Business_Objects.TheoDoiThiDua TheoDoiThiDua)
        {
            SQLUtility.TableName = "TheoDoiThiDua";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(TheoDoiThiDua)); }
            catch { return -1; }
        }


        public List<TheoDoiThiDua> GetTheoDoiThiDuaBySchoolIdAndSchoolYear(string SchoolYear, int schoolId)
        {
            DbParameter[] parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@SchoolYearID", SchoolYear);
            parameters[1] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectTheoDoiThiDuasByAndSchoolYearIDAndSchoolID", parameters);
            return MakeTheoDoiThiDuas(dt).ToList();  
        }
    }
}
